//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.config.service.impl;

import org.apache.commons.lang3.StringUtils;
import com.ht.modules.system.online.config.service.DbTableHandleI;
import com.ht.modules.system.online.config.util.ColumnMeta;

public class DbTableOracleHandleImpl implements DbTableHandleI {
    public DbTableOracleHandleImpl() {
    }

    public String getAddColumnSql(ColumnMeta columnMeta) {
        return " ADD  " + this.getAddFieldDesc(columnMeta) + "";
    }

    public String getReNameFieldName(ColumnMeta columnMeta) {
        return "RENAME COLUMN  " + columnMeta.getOldColumnName() + " TO " + columnMeta.getColumnName() + "";
    }

    public String getUpdateColumnSql(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return " MODIFY   " + this.getUpdateFieldDesc(cgformcolumnMeta, datacolumnMeta) + "";
    }

    public String getMatchClassTypeByDataType(String dataType, int digits) {
        String result = "";
        if (dataType.equalsIgnoreCase("varchar2")) {
            result = "string";
        }

        if (dataType.equalsIgnoreCase("nvarchar2")) {
            result = "string";
        } else if (dataType.equalsIgnoreCase("double")) {
            result = "double";
        } else if (dataType.equalsIgnoreCase("number") && digits == 0) {
            result = "int";
        } else if (dataType.equalsIgnoreCase("number") && digits != 0) {
            result = "double";
        } else if (dataType.equalsIgnoreCase("int")) {
            result = "int";
        } else if (dataType.equalsIgnoreCase("Date")) {
            result = "date";
        } else if (dataType.equalsIgnoreCase("Datetime")) {
            result = "date";
        } else if (dataType.equalsIgnoreCase("blob")) {
            result = "blob";
        } else if (dataType.equalsIgnoreCase("clob")) {
            result = "text";
        }

        return result;
    }

    public String dropTableSQL(String tableName) {
        return " DROP TABLE  " + tableName.toLowerCase() + " ";
    }

    public String getDropColumnSql(String fieldName) {
        return " DROP COLUMN " + fieldName.toUpperCase() + "";
    }

    private String getAddFieldDesc(ColumnMeta columnMeta) {
        String result = "";
        if (columnMeta.getColunmType().equalsIgnoreCase("string")) {
            result = columnMeta.getColumnName() + " varchar2(" + columnMeta.getColumnSize() + ")";
        } else if (columnMeta.getColunmType().equalsIgnoreCase("date")) {
            result = columnMeta.getColumnName() + " datetime";
        } else if (columnMeta.getColunmType().equalsIgnoreCase("int")) {
            result = columnMeta.getColumnName() + " NUMBER(" + columnMeta.getColumnSize() + ")";
        } else if (columnMeta.getColunmType().equalsIgnoreCase("double")) {
            result = columnMeta.getColumnName() + " NUMBER(" + columnMeta.getColumnSize() + "," + columnMeta.getDecimalDigits() + ")";
        } else if (columnMeta.getColunmType().equalsIgnoreCase("bigdecimal")) {
            result = columnMeta.getColumnName() + " NUMBER(" + columnMeta.getColumnSize() + "," + columnMeta.getDecimalDigits() + ")";
        } else if (columnMeta.getColunmType().equalsIgnoreCase("text")) {
            result = columnMeta.getColumnName() + " CLOB ";
        } else if (columnMeta.getColunmType().equalsIgnoreCase("blob")) {
            result = columnMeta.getColumnName() + " BLOB ";
        }

        result = result + (StringUtils.isNotEmpty(columnMeta.getFieldDefault()) ? " DEFAULT " + columnMeta.getFieldDefault() : " ");
        result = result + ("Y".equals(columnMeta.getIsNullable()) ? " NULL" : " NOT NULL");
        return result;
    }

    private String getUpdateFieldDesc(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        String result = "";
        String isnull = "";
        if (!datacolumnMeta.getIsNullable().equals(cgformcolumnMeta.getIsNullable())) {
            isnull = cgformcolumnMeta.getIsNullable().equals("Y") ? "NULL" : "NOT NULL";
        }

        if (cgformcolumnMeta.getColunmType().equalsIgnoreCase("string")) {
            result = cgformcolumnMeta.getColumnName() + " varchar2(" + cgformcolumnMeta.getColumnSize() + ")" + isnull;
        } else if (cgformcolumnMeta.getColunmType().equalsIgnoreCase("date")) {
            result = cgformcolumnMeta.getColumnName() + " date " + isnull;
        } else if (cgformcolumnMeta.getColunmType().equalsIgnoreCase("int")) {
            result = cgformcolumnMeta.getColumnName() + " NUMBER(" + cgformcolumnMeta.getColumnSize() + ") " + isnull;
        } else if (cgformcolumnMeta.getColunmType().equalsIgnoreCase("double")) {
            result = cgformcolumnMeta.getColumnName() + " NUMBER(" + cgformcolumnMeta.getColumnSize() + "," + cgformcolumnMeta.getDecimalDigits() + ") " + isnull;
        } else if (cgformcolumnMeta.getColunmType().equalsIgnoreCase("bigdecimal")) {
            result = cgformcolumnMeta.getColumnName() + " NUMBER(" + cgformcolumnMeta.getColumnSize() + "," + cgformcolumnMeta.getDecimalDigits() + ") " + isnull;
        } else if (cgformcolumnMeta.getColunmType().equalsIgnoreCase("blob")) {
            result = cgformcolumnMeta.getColumnName() + " BLOB " + isnull;
        } else if (cgformcolumnMeta.getColunmType().equalsIgnoreCase("text")) {
            result = cgformcolumnMeta.getColumnName() + " CLOB " + isnull;
        }

        result = result + (StringUtils.isNotEmpty(cgformcolumnMeta.getFieldDefault()) ? " DEFAULT " + cgformcolumnMeta.getFieldDefault() : " ");
        result = result + isnull;
        return result;
    }

    public String getCommentSql(ColumnMeta columnMeta) {
        return "COMMENT ON COLUMN " + columnMeta.getTableName() + "." + columnMeta.getColumnName() + " IS '" + columnMeta.getComment() + "'";
    }

    public String getSpecialHandle(ColumnMeta cgformcolumnMeta, ColumnMeta datacolumnMeta) {
        return null;
    }

    public String dropIndexs(String indexName, String tableName) {
        return "DROP INDEX " + indexName;
    }
}
